﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FGP_Kpaint
{
    class CheckGesture
    {
        public Gesture[] gestures;
        int l = 0;

        public CheckGesture()
        {

        }

        public void LoadGesture(MainWindow main)
        {
            Boolean T = true;

            int j = 0;

            int v = 0;

            int z = 0;
            string num = "";
            string myString = "";

            using (StreamReader sr = File.OpenText("D:\\ye.txt"))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    myString += s;
                }
            }

            l = 0;

            for (int i = 0; i < myString.Length; i++)
            {
                if (myString[i] == '+')
                {
                    l++;
                }

            }
            l = l + 1;
            //  main.label2.Content = "Count =" + l;
            gestures = new Gesture[l];
            for (int i = 0; i < l; i++)
            {

                gestures[i] = new Gesture();
            }

            for (int i = 0; i < myString.Length; i++)
            {
                // main.label2.Content = "Here =" + i;

                if (myString[i] != '+')
                {

                    if (myString[i] != ' ' && T == true)
                    {
                        gestures[j].Type += myString[i];

                    }


                    else if (myString[i] != ' ' && T == false)
                    {

                        num += myString[i];
                    }


                    else if (myString[i] == ' ' && T == false)
                    {
                        if (z >= 2)
                        {
                            z = 0;
                            v++;
                        }
                        gestures[j].points[v, z] = Convert.ToDouble(num);
                        // int l = gestures[j].points[v, z];
                        z++;

                        num = "";



                    }

                    else
                    {
                        T = false;


                    }


                }

                else
                {
                    T = true;
                    z = 0; v = 0;
                    j++;
                }


            }



        }


        ///function 

        public Boolean AddGesture(string type, double[,] points)
        {
            string lines = "+" + type + " ";

            // Compose a string that consists of three lines.
            for (int i = 0; i < 4; i++)
            {
                lines += points[i, 0] + " ";
                lines += points[i, 1] + " ";
                //  lines += points[i, 2] + " ";
            }
            // Write the string to a file.
            using (StreamWriter sw = File.AppendText("D:\\ye.txt"))
            {
                sw.WriteLine(lines);

            }



            return true;

        }



        public string checking(double[,] points)
        {
            string s = "";
            int count = 0;
            Boolean flag = false;

            for (int i = 1; i < l; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int z = 0; z < 2; z++)
                    {
                        if ((points[j, 0] < gestures[i].points[j, z] + 120 && points[j, 0] > gestures[i].points[j, z] - 120))
                        {

                            s = gestures[i].Type;
                            //flag = true;
                            count++;
                        }

                    }

                }
                if (count >= 4)
                {
                    count = 0;
                    return s;
                }
                else
                {
                    s = "Not Exist"; count = 0;
                }

            }

            return s;
        }

    }
}
